package com.kedacom.freedomLabel.base;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kedacom.freedomLabel.MainApplication;
import com.kedacom.freedomLabel.R;
import com.kedacom.lego.fast.util.ToastUtil;
import com.kedacom.lego.fast.view.LegoFastActivity;
import com.kedacom.lego.util.Utils;
import com.lzscale.scalelib.misclib.Misc;


public abstract class BaseActivity<V extends ViewDataBinding, VM extends BaseViewModel>
        extends LegoFastActivity<V, VM> {

    protected V mBinding;
    protected VM mViewModel;
    protected Misc mMisc;

    public static Dialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        mMisc = Misc.newInstance();
        mBinding = getViewDataBinding();
        mViewModel = getViewModel();
    }

    protected void beep() {
        mMisc.beep();
    }

    @Override
    public void showToast(String message) {
        ToastUtil.showFilteredDefaultToast(message);
    }

    @Override
    public void showToast(String message, int toastType) {
        ToastUtil.showFilteredToast(Utils.getApp(), message, toastType, Toast.LENGTH_LONG, 4);
    }

    @Override
    public int getBindingVariableId() {
        return com.kedacom.freedomLabel.BR.viewModel;
    }

    @Override
    public void showDialog(String message) {
        this.showDialog(message, getString(R.string.dialog_positive), (dialog, which) -> dialog.dismiss());
    }

    @Override
    public void showDialog(String message, String okButtonText, DialogInterface.OnClickListener buttonClickListener) {
        (new AlertDialog.Builder(this))
                .setTitle(R.string.dialog_tip_title)
                .setMessage(message)
                .setCancelable(true)
                .setPositiveButton(okButtonText, buttonClickListener)
                .create()
                .show();
    }

    public void showDialog(String message, String okButtonText, String cancelButtonText, DialogInterface.OnClickListener buttonClickListener) {
        (new AlertDialog.Builder(this))
                .setTitle(R.string.dialog_tip_title)
                .setMessage(message)
                .setCancelable(true)
                .setPositiveButton(okButtonText, buttonClickListener)
                .setNegativeButton(cancelButtonText, buttonClickListener)
                .create()
                .show();
    }

    public Dialog createLoadingDialog(Context context, String msg) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v
                .findViewById(R.id.dialog_loading_view);// 加载布局
        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
        tipTextView.setText(msg);// 设置加载信息

        loadingDialog = new Dialog(context, R.style.MyDialogStyle);// 创建自定义样式dialog
        loadingDialog.setCancelable(true); // 是否可以按“返回键”消失
        loadingDialog.setCanceledOnTouchOutside(false); // 点击加载框以外的区域
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        /**
         *将显示Dialog的方法封装在这里面
         */
        Window window = loadingDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.PopWindowAnimStyle);
        loadingDialog.show();
        return loadingDialog;
    }

    public void closeDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        ((MainApplication) getApplication()).addClickProxy(mBinding.getRoot());
    }

    public void startActivity(Class<?> targetActivity) {
        startActivity(getSkipIntent(targetActivity));
    }

    protected Intent getSkipIntent(Class<?> targetActivity) {
        return new Intent(getApplicationContext(), targetActivity);
    }






}